import axios from 'axios'
import store from '@/store'

// create an axios instance
const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,

  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // 判断当前是否有token
    if (store.getters.token) {
      config.headers['Authorization'] = `Bearer ${store.getters.token}`
    }
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 响应拦截器

request.interceptors.response.use(
  function (response) {
    // 解构一层data 方便调用
    const res = response.data
    // console.log('服务器返回的数据', res)

    return res
  },
  function (error) {
    return Promise.reject(error)
  }
)
export default request
